<template>
  <el-dialog :title="title + secondLabel" :visible="visible" @close="closeDialog">
    <el-form :model="orderData">
      <el-form-item label="订单编号" label-width="100px" required>
        <el-input v-model="orderData.orderSnId" disabled autocomplete="off"></el-input>
      </el-form-item>

      <slot></slot>

      <el-form-item :label="secondLabel" label-width="100px" required>
        <el-input v-model="orderData[editKey]" :min="0" :placeholder="placeholder" autocomplete="off" :type="iptType"
          @input="change"></el-input>
      </el-form-item>
    </el-form>

    <div slot="footer" class="dialog-footer center">
      <el-button type="primary" v-hasPermi="['finance:change:money']" @click="submitForm" :loading="submitLoading">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>

</template>

<script>
import { quotation } from '@/api/order/order'
export default {
  name: 'EditQutation',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    secondLabel: {
      type: String,
      default: "",
    },
    placeholder: {
      type: String,
      default: "",
    },
    qutationData: {
      type: Object,
      default: {}
    },
    editKey: {
      type: String,
      default: "",
    },
    URL: {
      type: String,
      default: "/order/order/quotation"
    },
    title: {
      type: String,
      default: "修改"
    },
    iptType: {
      type: String,
      default: "text"
    }
  },

  data() {
    return {
      orderData: {
        orderSnId: ''
      },
      submitLoading: false
    }
  },

  watch: {
    visible: {
      handler(newVal, oldVal) {
        if (newVal) {
          this.orderData = {
            id: this.qutationData.id || '',
            orderSnId: this.qutationData.orderSnId || '',
            [this.editKey]: this.qutationData[this.editKey] || '',
            returnTime: this.qutationData.returnTime || null,
            processed: this.qutationData.processed || null,
          }
        }
      },
      immediate: true,
      deep: true
    }
  },

  methods: {
    change(e) {
      this.orderData[this.editKey] = e.target.value
    },

    cancel() {
      this.$emit('close', false)
    }
  },
  methods: {
    closeDialog() {
      this.$emit('close', false)
    },

    submitForm() {
      this.submitLoading = true;
      quotation(this.orderData, this.URL).then(res => {
        this.$message.success(res.msg);
        this.$emit("receiveData", this.orderData, this.editKey, res.data || {});
        this.$emit("close", false);
      }).finally(() => {
        this.submitLoading = false;
      })
    },

    cancel() {
      this.$emit('close', false)
    },

    change(e) {
      this.$forceUpdate()
    }
  }
}
</script>

<style></style>
